package gobwas

import (
	"github.com/gobwas/ws"
	"github.com/gobwas/ws/wsutil"
	"github.com/sirupsen/logrus"
	"io/ioutil"
	"net/http"
	"strings"
)

type WsServer struct {
	Host string
}

func (this *WsServer) SimpleServe(){
	logrus.Info("listen host: ", this.Host)
	http.HandleFunc("/ws_msg", func(w http.ResponseWriter, r *http.Request){
		logrus.Info("get client msg...")
		conn, _, _, err := ws.UpgradeHTTP(r, w)
		if err != nil {
			logrus.Error("websocket connect error: ", err.Error())
		}

		go func(){
			defer conn.Close()
			for{
				msg, op, err := wsutil.ReadClientData(conn)
				if err != nil {
					logrus.Error("read client msg error: ", err.Error())
				}
				logrus.Info("receive message from client: ", string(msg))
				logrus.Info("receive op code: ", op.IsData())
				logrus.Info("receive conn: ", conn == nil)
				err = wsutil.WriteServerMessage(conn, op, []byte(strings.ToUpper(string(msg))))
				if err != nil {
					logrus.Error("write msg to client error: ", err.Error())
				}
			}
		}()
	})
	http.ListenAndServe(this.Host, nil)
}

func (this *WsServer) SimpleServeTwo(){
	logrus.Info("listen host: ", this.Host)
	http.HandleFunc("/ws_msg", func(w http.ResponseWriter, r *http.Request){
		logrus.Info("get client msg...")
		conn, _, _, err := ws.UpgradeHTTP(r, w)
		if err != nil {
			logrus.Error("websocket connect error: ", err.Error())
		}

		go func(){
			defer conn.Close()

			var (
				// the endpoint is server
				state = ws.StateServerSide
				reader = wsutil.NewReader(conn, state)
				// op parameter means the data format (text, binary, ping, pong ..)
				writer = wsutil.NewWriter(conn, state, ws.OpText)
			)

			for{
				// call next frame to prepare to read next message
				header, err := reader.NextFrame()
				logrus.Info("header params: ", header.OpCode, "|", header.Fin, "|", header.Length, "|", header.Mask, "|", header.Masked, "|", header.Rsv)
				if err != nil {
					logrus.Error("get header error: ", err.Error())
				}

				writer.Reset(conn, state, header.OpCode)
				data, err := ioutil.ReadAll(reader)

				if err != nil {
					logrus.Error("read data from reader error: ", err.Error())
				}

				logrus.Info("data from reader: ", string(data))


				/*_, err = io.Copy(writer, reader)
				if err != nil {
					logrus.Error("copy data from reader to writer error: ", err.Error())
				}*/

				writer.Write([]byte(strings.ToUpper(string(data))))
				err = writer.Flush()
				if err != nil {
					logrus.Error("write data error: ", err.Error())
				}
			}
		}()
	})
	http.ListenAndServe(this.Host, nil)
}

func (this *WsServer) OriginSimpleServe(){
	logrus.Info("OriginSimpleServe host: ", this.Host)
	http.ListenAndServe(this.Host, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		logrus.Info("get client msg...")
		conn, _, _, err := ws.UpgradeHTTP(r, w)
		if err != nil {
			logrus.Error("websocket connect error: ", err.Error())
		}
		go func() {
			defer conn.Close()

			for {
				msg, op, err := wsutil.ReadClientData(conn)
				logrus.Info("receive message from client: ", string(msg))
				if err != nil {
					logrus.Error("read client msg error: ", err.Error())
				}
				err = wsutil.WriteServerMessage(conn, op, []byte(strings.ToUpper(string(msg))))
				if err != nil {
					logrus.Error("write msg to client error: ", err.Error())
				}
			}
		}()
	}))
}